package com.android.fastride.view.fragment;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.style.TextAppearanceSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.fastride.Constant;
import com.android.fastride.R;
import com.android.fastride.contract.CompletedOrderListContract;
import com.android.fastride.model.db.entity.Order;
import com.android.fastride.present.CompletedOrderListPresent;
import com.chad.library.adapter.base.BaseViewHolder;
import com.peng.library.mantis.expand.list.MantisListFragment;
import com.peng.library.mantis.expand.list.link.ListConfig;
import com.peng.library.mantis.expand.refresh.link.RefreshConfig;
import com.peng.library.mantis.hint.HintConfig;
import com.peng.library.mantis.mvp.BindPresent;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

@BindPresent(CompletedOrderListPresent.class)
public class CompletedOrderListFragment extends MantisListFragment<CompletedOrderListContract.PRESENT,
        Order>
        implements CompletedOrderListContract.VIEW ,Constant, DatePickerFragment
        .SelectedDateListener{

    public static String TYPE = "mOrderStatus";
    private static final String REQUEST_DATE = "request_date";
    private static final int REQUEST_CODE_START_DATE = 1;
    private static final int REQUEST_CODE_END_DATE = 2;
    private SimpleDateFormat simpleDateFormat = new SimpleDateFormat("MM.dd.yyyy");
    private int mOrderStatus;

    Unbinder unbinder;
    @BindView(R.id.cl_datepick_layout)
    ConstraintLayout mConstraintLayout;
    @BindView(R.id.tv_start_date)
    TextView tvStartDate;
    @BindView(R.id.iv_pick_start_date)
    ImageView ivPickStartDate;
    @BindView(R.id.tv_end_date)
    TextView tvEndDate;
    @BindView(R.id.iv_pick_end_date)
    ImageView ivPickEndDate;
    @BindView(R.id.ib_search)
    ImageView ibSearch;
    @BindView(R.id.rv_message_list)
    RecyclerView rvMessageList;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        if (bundle != null) {
            mOrderStatus =  bundle.getInt(ORDER_STATUS, 0);
        }
    }

    public static CompletedOrderListFragment newInstance(int orderStatus) {
        CompletedOrderListFragment completedOrderListFragment = new CompletedOrderListFragment();
        Bundle bundle = new Bundle();
        bundle.putInt(ORDER_STATUS, orderStatus);
        completedOrderListFragment.setArguments(bundle);
        return completedOrderListFragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState,
                             HintConfig.Builder hintConfigBuilder,
                             RefreshConfig.Builder refreshConfigBuilder,
                             ListConfig.Builder listConfigBuilder) {
        View view = inflater.inflate(R.layout.fragment_completed_order, container, false);
        unbinder = ButterKnife.bind(this, view);
//        hintConfigBuilder.layoutEmptyId(R.layout.hint_empty);
        hintConfigBuilder.hintEnable(false);
        refreshConfigBuilder.needRefreshViewId(R.id.rv_message_list);
        refreshConfigBuilder.refreshEnable(true);
        refreshConfigBuilder.autoRefresh(true);
        refreshConfigBuilder.pullToRefresh(true);
        listConfigBuilder.recyclerViewId(R.id.rv_message_list);
        listConfigBuilder.canLoadMore(true);
        listConfigBuilder.hasDivider(false);

        initView();
        return view;
    }

    private void initView() {
        mConstraintLayout.setVisibility(View.VISIBLE);
    }

    @Override
    public void onRetry(int type, Object tag) {
        super.onRetry(type, tag);
        getPresent().onRefreshCall();
        getViewDelegate().showProgressView();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
            unbinder = null;
        }

    }



    @Override
    public void onConvert(BaseViewHolder holder, Order order, int position) {

    }

    @Override
    public void onReceiveError(Throwable throwable) {

    }

    @Override
    public int getItemLayout() {
//        return R.layout.list_item_receive_of_delivery;
        return R.layout.list_item_completed_order;
    }

    @Override
    public int getOrderStatus() {
        return mOrderStatus;
    }

    @OnClick({R.id.tv_start_date, R.id.iv_pick_start_date, R.id.tv_end_date, R.id
            .iv_pick_end_date, R.id.ib_search})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tv_start_date:
            case R.id.iv_pick_start_date:
                pickerDate(REQUEST_CODE_START_DATE, stringToDate(tvStartDate.getText().toString()));
                break;
            case R.id.tv_end_date:
            case R.id.iv_pick_end_date:
                pickerDate(REQUEST_CODE_END_DATE, stringToDate(tvEndDate.getText().toString()));
                break;
            case R.id.ib_search:
                break;
        }

    }

    private void pickerDate(int requestCode, Date date) {
        DatePickerFragment datePickerFragment = DatePickerFragment.newInstance(date);
        datePickerFragment.setSelectedDateListener(this, requestCode);
        datePickerFragment.show(getFragmentManager(), REQUEST_DATE);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Date date = (Date) data.getSerializableExtra(DatePickerFragment.EXTRA_DATE);
        if (resultCode != Activity.RESULT_OK || date == null) {
            return;
        }

        if (requestCode == REQUEST_CODE_START_DATE) {
            tvStartDate.setText(dateToString(date));
        } else if (requestCode == REQUEST_CODE_END_DATE) {
            tvEndDate.setText(dateToString(date));
        }

    }

    public String dateToString(Date date) {
        return simpleDateFormat.format(date);
    }

    public Date stringToDate(String dateStr) {
        Date date = null;
        try {
            date = simpleDateFormat.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    private void setTextStyle(TextView view, int val) {
        setTextStyle(view, String.valueOf(val));
    }

    private void setTextStyle(TextView view, float val) {
        setTextStyle(view, String.valueOf(val));
    }

    private void setTextStyle(TextView view, String val) {
        String values = null;
        if (val.contains(".")) {
            values = val;
        } else {
            values = val + ".00";
        }
        int position = values.indexOf(".");
        SpannableString styleText = new SpannableString(values);
        styleText.setSpan(new TextAppearanceSpan(getContext(), R.style.bigGreenTextStyle), 0,
                position + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        styleText.setSpan(new TextAppearanceSpan(getContext(), R.style.smallGreenTextStyle),
                position + 1, values.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        view.setText(styleText, TextView.BufferType.SPANNABLE);
    }

    @Override
    public void onSelectedDate(int requestCode, Date date) {
        if (requestCode == REQUEST_CODE_START_DATE) {
            tvStartDate.setText(dateToString(date));
        } else if (requestCode == REQUEST_CODE_END_DATE) {
            tvEndDate.setText(dateToString(date));
        }
    }
}
